# _*_ encoding:utf-8 _*_
"""
机构的模型
"""
# python 源生包
from datetime import datetime
# 第三方库
from django.db import models
# 自定义的包
# Create your models here.


# 城市
class CityDict(models.Model):
    name = models.CharField(max_length=20, verbose_name="城市名")
    desc = models.CharField(max_length=200, verbose_name="城市描述")
    add_time = models.DateField(default=datetime.now, verbose_name="添加时间")

    class Meta:
        verbose_name = "城市"
        verbose_name_plural = verbose_name

    # 如果这个 model 为其它 model 的外键，需要加入这个，不然会显示 object
    def __str__(self):
        return self.name


# 机构
class CourseOrg(models.Model):
    city = models.ForeignKey(CityDict, verbose_name="所在城市", on_delete=models.DO_NOTHING)  # 外键
    name = models.CharField(max_length=50, verbose_name="机构名称")
    desc = models.TextField(verbose_name="机构描述")
    category = models.CharField(max_length=20, choices=(("pxjg", "培训机构"), ("gr", "个人"), ("gx", "高校")), verbose_name="机构类别", default="pxjg")
    click_num = models.IntegerField(default=0, verbose_name="机构点击量")
    av_nums = models.IntegerField(default=0, verbose_name="收藏人数")
    image = models.ImageField(upload_to="org/%Y/%m", verbose_name="封面图", max_length=100)
    address = models.CharField(max_length=150, verbose_name="机构地址")
    students = models.IntegerField(default=0, verbose_name="学习人数")
    course_nums = models.IntegerField(default=0, verbose_name="课程数")
    add_time = models.DateField(default=datetime.now, verbose_name="添加时间")

    class Meta:
        verbose_name = "课程机构"
        verbose_name_plural = verbose_name

    # 因为机构是教师的外键，所以可以通过以下方法，反向获取外键的数据
    def get_teacher_nums(self):
        # self.teacher_set 为 django 自动生成
        # self.teacher_set.all() 是一个 query dict
        # 视图中可以通过 course.get_zj_nums() 调用
        return self.teacher_set.all().count()

    # 如果这个 model 为其它 model 的外键，需要加入这个，不然会显示 object
    def __str__(self):
        return self.name


# 教师
class Teacher(models.Model):
    org = models.ForeignKey(CourseOrg, verbose_name="所属机构", on_delete=models.DO_NOTHING)  # 外键
    name = models.CharField(max_length=50, verbose_name="教师名")
    work_years = models.IntegerField(default=0, verbose_name="工作年限")
    work_company = models.CharField(max_length=50, verbose_name="就职公司")
    work_position = models.CharField(max_length=50, verbose_name="工作职位")
    point = models.CharField(max_length=50, verbose_name="教学特点")
    click_nums = models.IntegerField(default=0, verbose_name="点击量")
    fav_nums = models.IntegerField(default=0, verbose_name="收藏人数")
    add_time = models.DateField(default=datetime.now, verbose_name="添加时间")

    class Meta:
        verbose_name = "教师"
        verbose_name_plural = verbose_name

    # 如果这个 model 为其它 model 的外键，需要加入这个，不然会显示 object
    def __str__(self):
        return self.name
